# Change the working directory to the one with your outputs.

# Reading random effects results
b<-matrix(scan("bp1j2.out"),ncol=26,byrow=TRUE)

# checking acceptance probabilities
sum(b[,2]>b[,3])/nrow(b)
sum(b[,10]>b[,11])/nrow(b)

# checking convergence of some randomly chosen random effects

par(mfrow=c(2,3))

plot(b[,1],b[,4],xlab="Iteration",ylab="bs0",type='n')
lines(b[,1],b[,4])

plot(b[,1],b[,5],xlab="Iteration",ylab="bs1",type='n')
lines(b[,1],b[,5])

plot(b[,1],b[,12],xlab="Iteration",ylab="b0",type='n')
lines(b[,1],b[,12])

plot(b[,1],b[,13],xlab="Iteration",ylab="b1",type='n')
lines(b[,1],b[,13])

plot(b[,1],b[,14],xlab="Iteration",ylab="b2",type='n')
lines(b[,1],b[,14])

plot(b[,1],b[,22],xlab="Iteration",ylab="b10",type='n')
lines(b[,1],b[,22])

# reading deltastars and deltas

d<-matrix(scan("dp1j2.out"),ncol=40,byrow=TRUE)

# mean values of some randomly chosen delta/deltastar values
mean(d[,1])
mean(d[,25])
mean(d[,33])

# reading parameter estimates
rest2<-matrix(scan("tp1j2.out"),ncol=54,byrow=TRUE)

# removing the first 20*5=100 iterations as burn-in
rest2<-rest2[-c(1:20),]

rest2[,1]<-1:400

# checking acceptances
# beta
sum(rest2[,2]>rest2[,3])/nrow(rest2)

# betas
sum(rest2[,19]>rest2[,20])/nrow(rest2)

# alpha
sum(rest2[,36]>rest2[,37])/nrow(rest2)

# lambda

sum(rest2[,45]>rest2[,46])/nrow(rest2)

# lambdas

sum(rest2[,48]>rest2[,49])/nrow(rest2)

dim(rest2)

# checking convergences of some randomly chosen parameters 
par(mfrow=c(2,3))

plot(rest2[,1],rest2[,4],xlab="Iteration",ylab="Beta0",type='n')
lines(rest2[,1],rest2[,4])

plot(rest2[,1],rest2[,5],xlab="Iteration",ylab="Beta1",type='n')
lines(rest2[,1],rest2[,5])

plot(rest2[,1],rest2[,6],xlab="Iteration",ylab="Beta2",type='n')
lines(rest2[,1],rest2[,6])

plot(rest2[,1],rest2[,7],xlab="Iteration",ylab="Beta3",type='n')
lines(rest2[,1],rest2[,7])

plot(rest2[,1],rest2[,8],xlab="Iteration",ylab="Beta4",type='n')
lines(rest2[,1],rest2[,8])

plot(rest2[,1],rest2[,9],xlab="Iteration",ylab="Beta5",type='n')
lines(rest2[,1],rest2[,9])


par(mfrow=c(2,3))

plot(rest2[,1],rest2[,10],xlab="Iteration",ylab="Beta6",type='n')
lines(rest2[,1],rest2[,10])

plot(rest2[,1],rest2[,11],xlab="Iteration",ylab="Beta7",type='n')
lines(rest2[,1],rest2[,11])

plot(rest2[,1],rest2[,12],xlab="Iteration",ylab="Beta8",type='n')
lines(rest2[,1],rest2[,12])

plot(rest2[,1],rest2[,13],xlab="Iteration",ylab="Beta9",type='n')
lines(rest2[,1],rest2[,13])

plot(rest2[,1],rest2[,17],xlab="Iteration",ylab="Beta14",type='n')
lines(rest2[,1],rest2[,17])

plot(rest2[,1],rest2[,18],xlab="Iteration",ylab="Beta15",type='n')
lines(rest2[,1],rest2[,18])



par(mfrow=c(2,3))

plot(rest2[,1],rest2[,21],xlab="Iteration",ylab="Betas0",type='n')
lines(rest2[,1],rest2[,21])

plot(rest2[,1],rest2[,22],xlab="Iteration",ylab="Betas1",type='n')
lines(rest2[,1],rest2[,22])

plot(rest2[,1],rest2[,27],xlab="Iteration",ylab="Betas6",type='n')
lines(rest2[,1],rest2[,27])

plot(rest2[,1],rest2[,28],xlab="Iteration",ylab="Betas7",type='n')
lines(rest2[,1],rest2[,28])

plot(rest2[,1],rest2[,29],xlab="Iteration",ylab="Betas8",type='n')
lines(rest2[,1],rest2[,29])

plot(rest2[,1],rest2[,30],xlab="Iteration",ylab="Betas9",type='n')
lines(rest2[,1],rest2[,30])

par(mfrow=c(2,3))

plot(rest2[,1],rest2[,38],xlab="Iteration",ylab="Alpha2",type='n')
lines(rest2[,1],rest2[,38])

plot(rest2[,1],rest2[,39],xlab="Iteration",ylab="Alpha3",type='n')
lines(rest2[,1],rest2[,39])

plot(rest2[,1],rest2[,40],xlab="Iteration",ylab="Alpha4",type='n')
lines(rest2[,1],rest2[,40])

plot(rest2[,1],rest2[,41],xlab="Iteration",ylab="Sigma1",type='n')
lines(rest2[,1],rest2[,41])

plot(rest2[,1],rest2[,42],xlab="Iteration",ylab="Sigma2",type='n')
lines(rest2[,1],rest2[,42])

plot(rest2[,1],rest2[,43],xlab="Iteration",ylab="Sigma3",type='n')
lines(rest2[,1],rest2[,43])


par(mfrow=c(2,3))

plot(rest2[,1],rest2[,44],xlab="Iteration",ylab="Sigma4",type='n')
lines(rest2[,1],rest2[,44])

plot(rest2[,1],rest2[,47],xlab="Iteration",ylab="Lamda1",type='n')
lines(rest2[,1],rest2[,47])

plot(rest2[,1],rest2[,50],xlab="Iteration",ylab="Lambda4",type='n')
lines(rest2[,1],rest2[,50])

# removing entries such as iteration number, acceptance probs etc to
# calculate posterior descriptives
rest2<-rest2[,-c(1:3,19,20,36,37,45,46,48,49,51:54)]

rest2<-t(rest2)

qrest2<-matrix(c(rep(0,195)),ncol=5)

for(i in 1:39)
qrest2[i,]<-c(quantile(rest2[i,],c(0.025,0.5,0.975)),mean(rest2[i,]),sqrt(var(rest2
[i,])))


dimnames(qrest2)<-list(c("b.Int","b.Age","b.Gender","b.Valves","b.weight","b.trans","b.passcap","b.origin","b.lugcap","b.resp","b.fuelcap","b.acceleration","b.noise","b.mileage","b.fuelcons",

"bs.Int","bs.Age","bs.Gender","bs.Valves","bs.weight","bs.trans","bs.passcap","bs.origin","bs.lugcap","bs.resp","bs.fuelcap","bs.acceleration","bs.noise","bs.mileage","bs.fuelcons",

"alpha2;1","alpha3;1","alpha4;1","log.sigma2.1", "log.sigma2.2", "log.sigma2.3","log.sigma2.4","lambda2","lambda2*"),c("2.5%","50%","97.5%","mean","SE"))

round(qrest2,2)

